﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RBOSS.Business.Checkout;
using RBOSS.Data;

namespace RBOSS.Business.CreditCard
{
    public class CreditCardSubsystem:ICreditCardSubsystem
    {
        readonly CreditCardDBEntities _dataContext = new CreditCardDBEntities();
        readonly RBOSSEntities _rbossEntities = new RBOSSEntities();

        public bool IsValidCreditCard(ICreditCard billingInfo,decimal amount)
        {
            var creditCard = (from c in _dataContext.CreditCards
                where
                    c.CreditCardNo == billingInfo.CreditCardNumber && c.ExpiryDate == billingInfo.ExpiredDate &&
                    c.SecurityPin == billingInfo.SecurityNumber
                select c).FirstOrDefault();

            return (creditCard != null) && (creditCard.Balance >= amount);
        }

        public ICreditCard GetCreditCard()
        {
            return new CreditCard();
        }

        public bool DeductAmount(ICreditCard billingInfo, decimal amount)
        {
            try
            {
                var creditCard = (from c in _dataContext.CreditCards
                                 where
                                     c.CreditCardNo == billingInfo.CreditCardNumber && c.ExpiryDate == billingInfo.ExpiredDate &&
                                     c.SecurityPin == billingInfo.SecurityNumber
                                 select c).FirstOrDefault();
                if (creditCard != null)
                {
                    creditCard.Balance = creditCard.Balance - amount;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public void AddCreditCardInformation(ICreditCard creditCard)
        {
            var objCreditCardToSave = new CreditCardInformation
            {
                AddressLine1 = creditCard.Address1,
                AddressLine2 = creditCard.Address2,
                City = creditCard.City,
                Country = creditCard.Country,
                CreditCardNumber = creditCard.CreditCardNumber,
                CreditTypeId = creditCard.CreditTypeId,
                ExpirationDate = creditCard.ExpiredDate,
                IsRemoved = creditCard.IsRemoved,
                Pin = creditCard.SecurityNumber,
                PrintedNameOnCard = creditCard.NameOnCard,
                State = creditCard.State,
                UserId = creditCard.UserId,
                Zip = creditCard.Zip
            };
            _rbossEntities.CreditCardInformations.AddOrUpdate(objCreditCardToSave);
            _rbossEntities.SaveChanges();
        }

        public IList<CreditCardType> GetAllCreditCardTypes()
        {
            var lstCreditCardType = from cardTypes in _rbossEntities.CreditCardTypes
                select new CreditCardType
                       {
                           CreditCardTypeId = cardTypes.CreditCardTypeId,
                           CreditCardTypeName = cardTypes.CreditCardTypeName,
                           Description = cardTypes.CreditCardTypeDescription
                       };
            return lstCreditCardType.ToList();
        }

        public List<CreditCard> GetValidCustomerCreditCards(int customerId)
        {
            var creditCardOfCustomer = from creditCards in _rbossEntities.CreditCardInformations
                where creditCards.UserId == customerId && creditCards.IsRemoved == false
                select new CreditCard
                       {
                           CreditCardId = creditCards.CreditCardId,
                           UserId = (int) creditCards.UserId,
                           Zip = creditCards.Zip,
                           CreditTypeId = creditCards.CreditTypeId,
                           City = creditCards.City,
                           Country = creditCards.Country,
                           CreditCardNumber = creditCards.CreditCardNumber,
                           IsRemoved = creditCards.IsRemoved,
                           State = creditCards.State,
                           Address1 = creditCards.AddressLine1,
                           Address2 = creditCards.AddressLine2,
                           ExpiredDate = creditCards.ExpirationDate,
                           NameOnCard = creditCards.PrintedNameOnCard,
                           SecurityNumber = creditCards.Pin
                       };

            return creditCardOfCustomer.ToList();
        }

        public void DeleteCreditCard(int creditCardId)
        {
            var creditCard = _rbossEntities.CreditCardInformations.FirstOrDefault(x => x.CreditCardId == creditCardId);

            if (creditCard != null)
            {
                creditCard.IsRemoved = true;
                _rbossEntities.CreditCardInformations.AddOrUpdate(creditCard);
                _rbossEntities.SaveChanges();
            }
        }

        public int GetCreditCardUpperBound()
        {
            var maxCreditCardAllowed = _rbossEntities.Settings.Select(x => x.MaxCreditCard).FirstOrDefault();
            if (maxCreditCardAllowed != null)
                return (int) maxCreditCardAllowed;
            return 0;
        }
    }
}
